package com.ledim.activity;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.base.LedimBaseListActivity;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.app.LeDimApplication;
import com.ledim.bean.LedimPushBean;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/10/25.
 * 新的 push 消息列表activity
 */

public class NewPushMsgActivity extends LedimBaseListActivity {

    @Override
    protected LedimBaseListAdapter getAdapter() {
        return new NewPushMgsAdapter(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = findViewByIdExt(R.id.tv_title);
        textView.setText("消息");
        LedimSpUtils.setPushMgsCount(0);
        ImageView mBack = findViewByIdExt(R.id.iv_back);
        mBack.setVisibility(View.VISIBLE);
        NotificationManager man = (NotificationManager) LeDimApplication.getInstance()
                                                                        .getApplicationContext()
                                                                        .getSystemService(Context.NOTIFICATION_SERVICE);
        man.cancelAll();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ((ListView) mAdapterView).setDividerHeight(DensityUtil.dip2px(this, 1));
    }

    @Override
    protected void getDatas() {
        LedimApiServiceFactory.createService(ApiService.class)
                              .getPushMsgList(mLoadPage + 1, getPageSize())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseListResponse<LedimPushBean>>() {

                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();

                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      setServerDataError();
                                      LogUtil.e(e.getMessage());

                                  }

                                  @Override
                                  public void onNext(BaseListResponse<LedimPushBean> dataInfo) {
                                      super.onNext(dataInfo);
                                      if (dataInfo != null && dataInfo.success) {
                                          initTotalPages(dataInfo.paged.total);
                                          setDatas(dataInfo.data, dataInfo.paged.more);
                                      } else {
                                          setServerDataError();
                                      }
                                  }
                              });
    }

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_pushmsg_list;
    }

    @Override
    protected boolean enablePullRefresh() {
        return false;
    }

    class NewPushMgsAdapter extends LedimBaseListAdapter<LedimPushBean> {

        public NewPushMgsAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.message_item, null);
                holder.mTitle = (TextView) convertView.findViewById(R.id.tv_msg_title);
                holder.mTime = (TextView) convertView.findViewById(R.id.tv_msg_time);
                holder.mContent = (TextView) convertView.findViewById(R.id.tv_msg_content);
                holder.mMsgItem = (LinearLayout) convertView.findViewById(R.id.ll_msg_item);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final LedimPushBean ledimPushBean = getItem(position);

            if (ledimPushBean != null) {
                holder.mTitle.setText(ledimPushBean.title);
                holder.mTime.setText(ledimPushBean.time);
                holder.mContent.setText(ledimPushBean.content);

                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //设置跳转
                        Intent pushMsgIntent =
                                new Intent(NewPushMsgActivity.this, SplashActivity.class);
                        pushMsgIntent.putExtra(ConstantUtil.DATA, ledimPushBean);
                        startActivity(pushMsgIntent);
                    }
                });
            }

            return convertView;
        }
    }

    public static class ViewHolder {

        TextView mTitle;
        TextView mTime;
        TextView mContent;
        LinearLayout mMsgItem;
    }
}
